<?php
$columns = (int)$this->form->columns;
$form = $this->form->getForm();
$column1width = (int)$form->column1width;
$column2width = (int)$form->column2width;
?>
<div class="RM_myreservations_container">
<?php
if ( $columns == 2 ){ ?>
<table cellpadding="0" cellspacing="0" border="0">
<tr><td valign="top" <?php if ($column1width > 0) echo "width='".$column1width."px'";?>>
    <table cellpadding="2" cellspacing="2" border="0">
        <?php foreach ($this->state[0] as $panel) { ?>
            <tr><td>
                <?php echo $this->partial('MyReservations/List/'.$panel->id.'.phtml', array('panel' => $panel, 'user' => $this->user)); ?>
            </td></tr>
        <?php } ?>
    </table>
</td><td valign="top" <?php if ($column2width > 0) echo "width='".$column2width."px'";?>>
    <table cellpadding="2" cellspacing="2" border="0">
        <?php foreach ($this->state[1] as $panel) { ?>
            <tr><td>
                <?php echo $this->partial('MyReservations/List/'.$panel->id.'.phtml', array('panel' => $panel, 'user' => $this->user)); ?>
            </td></tr>
        <?php } ?>
    </table>
</td></tr>
</table>
<?php } else { ?>
    <table cellpadding="2" cellspacing="2" border="0">
    <?php foreach ($this->state[0] as $panel) { ?>
        <tr><td <?php if ($column1width > 0) echo "width='".$column1width."px'";?>>
            <?php echo $this->partial('MyReservations/List/'.$panel->id.'.phtml', array('panel' => $panel, 'user' => $this->user, 'width' => $column1width)); ?>
        </td></tr>
    <?php } ?>
    </table>
<?php } ?>
</div>

<script type="text/javascript">

// set up default variable for start/end date
var dateObject = new Date();
var Year = dateObject.getFullYear();
var Month = dateObject.getMonth();
var Day = dateObject.getDate();
var RM_MyReservations_Selected_startDate = Year + "-" + Month + "-" + Day;
var RM_MyReservations_Selected_endDate = Year + "-" + Month + "-" + Day;
var RM_MyReservations_Selected_unit_id = 1;

window.onLoad = function(){
    
    /**
     * Show loading mask while some ajax requests
     */
    RM_MyReservations_LoadMask = function()
    {
        myMask = new Ext.LoadMask('RM_myreservations_container', {msg:RM.Translate.Common.PleaseWait});
        myMask.show();
    }

    RM_MyReservations_HideMask = function()
    {
        myMask.hide();
    }

    Ext.Ajax.on('beforerequest', RM_MyReservations_LoadMask, this);
    Ext.Ajax.on('requestcomplete', RM_MyReservations_HideMask, this);
    Ext.Ajax.on('requestexception', RM_MyReservations_HideMask, this);
};

/**
 * JS Print function
 */
RM_MyReservations_List_Print = function(){
    if(typeof(RM_MyReservations_List_GetSelected) === 'undefined') {
        return;
    }

    var selectedRow = RM_MyReservations_List_GetSelected();
    if (selectedRow == null) {
        return;
    }

    var newWindow = window.open(RM.Common.AssembleURL({
        controller : 'MyReservations_Print',
        action: 'showJson',
        parameters : [{
            name : 'reservationID',
            value : selectedRow.reservation_id
        }]
    }));
    newWindow.print();
}

/**
 * JS Cancel Reservation Function
 */
RM_MyReservations_List_Cancel = function(){
    //We need to check: if list panel is on the page setup and this method should exist
    if(typeof(RM_MyReservations_List_GetSelected) === 'undefined') {
        return;
    }

    var selectedRow = RM_MyReservations_List_GetSelected();
    if (selectedRow == null) {
        return;
    }

    if (selectedRow.cancel == 'pending') {
        Ext.Msg.alert(
            RM.Translate.Common.Status,
            RM.Translate.Common.MyReservations.Status.Errors.ThisReservationIsAlreadyInPendingStatus
        );
        return;
    }

    if (selectedRow.cancel == 'canceled') {
        Ext.Msg.alert(
            RM.Translate.Common.Status,
            RM.Translate.Common.MyReservations.Status.Errors.ThisReservationIsAlreadyCanceled
        );
        return;
    }

    if (confirm(RM.Translate.User.MyReservations.Cancel.AreYouSureYouWantToCancelThisReservation)) {
        Ext.Ajax.request({
            url : RM.Common.AssembleURL({
                controller: 'MyReservations_List',
                action: 'cancelJson'
            }),
            params: {
                reservationID: selectedRow.reservation_id
            },
            method: 'POST',
            success: function(responseObject) {
                RM_MyReservations_List_Reload();
            },
            failure: function() {
                Ext.Msg.alert('Status', RM.Translate.Common.UnableToShow);
            }
        });
    }
}

RM_MyReservations_List_Pay_URL = "<?php echo RM_Environment::getInstance()->getRouter()->_('Reservations', 'summaryvalidate'); ?>";

/**
 * JS Pay Balance Payment Function
 */
RM_MyReservations_List_Pay = function(){
    //We need to check: if list panel is on the page setup and this method should exist
    if(typeof(RM_MyReservations_List_GetSelected) === 'undefined') {
        return;
    }

    var selectedRow = RM_MyReservations_List_GetSelected();
    if (selectedRow == null) {
        return;
    }

    if (selectedRow.cancel == 'pending') {
        Ext.Msg.alert(
            RM.Translate.Common.Status,
            RM.Translate.Common.MyReservations.Status.Errors.ThisReservationIsAlreadyInPendingStatus
        );
        return;
    }

    if (selectedRow.cancel == 'canceled') {
        Ext.Msg.alert(
            RM.Translate.Common.Status,
            RM.Translate.Common.MyReservations.Status.Errors.ThisReservationIsAlreadyCanceled
        );
        return;
    }

    var reservationID = selectedRow.reservation_id;
    document.getElementById('reservationID').value = reservationID;
    document.getElementById('rm_myreservations_pay_form').submit();
    return true;
}

/**
 * JS View on Map function
 */
RM_MyReservations_List_View_Url = "<?php echo RM_Environment::getInstance()->getRouter()->_('GMaps', 'map', array("unit_id" => '-UNITID-', "page" => "map.phtml")) ?>";
RM_MyReservations_List_View = function(){
    if(typeof(RM_MyReservations_List_GetSelected) === 'undefined') {
        return;
    }

    var selectedRow = RM_MyReservations_List_GetSelected();
    if (selectedRow == null) {
        return;
    }
    RM_doShadowBox(RM_MyReservations_List_View_Url.replace('-UNITID-', selectedRow.unit_id), '', 500, 400, 'iframe' );

}
</script>